//本页面由uniapp切片工具生成，uni-app切片-可视化设计工具(一套代码编译到7个平台iOS、Android、H5、小程序)，软件下载地址：http://www.ymznkf.com/new_view_669.htm
//import app from "../../App.vue"


// 定义全局参数,控制数据加载
var _self, page = 1,
	timer = null;

export default {
	name: 'recharge',
	data() {
		return {
			loadingText: '',
			animation: '',
			animationData: {},
			vipLevelId: '', 		//会员类型ID
			vipLevelName: '',		//        名称
			money: '0.00', 			//充值金额
			payType: '', 			//付款方式(0-微信, 1-支付宝)


			page: 0, //当前分页页码
			apiUrl: 'https://www.ymznkf.com/wx_server', //后端接口地址
			id: '', //传值使用,方便存在本地的locakStorage  
			del_id: '', //方便存在本地的locakStorage  

			vipLevels: [], //会员种类
			orderInfo2: '',
			requestPaymentSuccess: '',
			requestPaymentFail: '',
			requestPaymentComplate: '',
		}
	},
	components: {

	},
	onLoad(options) {
		_self = this;

		//检查是否登录参考代码,需要用的时候，可以把注释取掉
		//if(this.checkLogin()==false){
		//	return;
		//}

		//this.getLaction();//得到gps

		this.page = 0;

		//检测有没有传入id参数
		if (options.id != null && options.id != "") {
			this.id = options.id;
		}

		// 初始化一个动画
		var animation = uni.createAnimation({
			transformOrigin: "50% 0 50%",
			duration: 1000, //动画持续1秒
			timingFunction: 'linear', //linear 全程匀速运动
			delay: 200 //延迟两秒执行动画
		})
		this.animation = animation;
		this.scaleAndScale();

		//执行初始化,需要用的时候，可以把注释取掉
		//this.Refresh("init");

	},
	onShow() {
		//console.log("on show");
		this.loadVipLevels()
		//if(this.checkLogin()==false){
		//	return;
		//}

		//执行初始化,需要用的时候，可以把注释取掉
		//this.Refresh("init");
	},
	onPullDownRefresh: function() {
		//下拉刷新的时候请求一次数据
		this.Refresh();
	},
	methods: {
		//会员种类-加载
		loadVipLevels() {
			let that = this
			that.$http.get("/viplevel/vipInfo/list", {}).then(res => {
				if (res.data.success) {
					let _rv = res.data.result.records
					if (!_rv) {
						that.$tip.toast('会员充值维护中...');
						return false
					}
					for (let i = 0; i < _rv.length; i++) {
						that.vipLevels.push({
							id: _rv[i].id,
							name: _rv[i].vipName,
							price: _rv[i].vipPrice,
							expire: _rv[i].expire,
							expireDay: _rv[i].expireDay,
							isXufei: _rv[i].isXufei,
							active: false
						})
					}
					that.vipLevels = _rv
				} else {
					that.$tip.toast(res.data.message);
				}
			}).catch(e => {
				//that.$tip.error(COMMON_HTTP_ERROR_TIP);
			});
		},
		//会员种类-选择
		choseVipLevel(item) {
			if (!item) {
				return false;
			}

			if (item.active) {
				//高亮点击了自己表示取消当前
				item.active = false
				this.vipLevelId = ''
				this.vipLevelName = ''
				this.money = 0.00
				return false;
			} else {
				//点击了其它的
				for (let i = 0; i < this.vipLevels.length; i++) {
					this.vipLevels[i].active = false;
				}
				item.active = true
				this.vipLevelId = item.id
				this.vipLevelName = item.vipName
				this.money = item.vipPrice
			}
		},
		//切换支付方式
		changePayType(type) {
			if (type == this.payType) {
				//选择了本身, 表示取消重新选择
				this.payType = ''
				return false
			}
			this.payType = type
		},
		// 定义动画内容
		scaleAndScale() {
			// 定义动画内容
			this.animation.opacity(0, 1).step() //先放大1,2倍
			this.animation.opacity(1, 0).step() //缩小至原来的大小
			// 导出动画数据传递给data层
			this.animationData = this.animation.export(); //每次执行导出动画时 会覆盖之前的动画
		},
		noscaleAndScale() {
			this.animation.scale(1, 1).step()
			this.animationData = this.animation.export()
		},
		//提交订单
		submitOrder() {
			if (!this.vipLevelId) {
				uni.showToast({
					title: '请选择会员类型',
					icon: 'none',
					duration: 1000
				});
				return false;
			}
			if (this.payType == '') {
				uni.showToast({
					title: '请选择付款方式',
					icon: 'none',
					duration: 1000
				});
				return false;
			}
			
			//向本平台提交订单信息
			/*
			创建订单向后台传参:
				orderType: 订单类型(0-会员充值场景, 1-非会员按次消费服务项目)
				payType: 支付方式(0-微信, 1-支付宝)
					orderType=0时, 额外回传[vipLevelId: 会员类型ID]
					orderType=1时, 额外回传[serviceItemId: 服务项目ID]
			*/
			let _params = {
				orderType: '0',
				payType: this.payType,
				vipLevelId: this.vipLevelId
			}
			uni.showLoading({
				title: '订单处理中...',
				mask: true,
			})
			let that = this
			that.$http.post("/orderinfo/orders/add", _params).then(res=>{
				if(res.data.success){
					uni.hideLoading()
					let serverJsonData = res.data.result
					if(!serverJsonData){
						that.$tip.toast('下单失败,刷新重试或联系客服处理')
						return false
					}
					if (that.payType == '0') {
						//微信付款
						that.useWeiXinPay(serverJsonData)
					} else if (that.payType == '1') {
						//支付宝付款
						that.useZhiFuBaoPay(serverJsonData)
					} else {
					}
				}else{
					uni.hideLoading()
					that.$tip.toast(res.data.message);	
				}
			}).catch(err => {
				uni.hideLoading()
			});
		},

		//微信付款
		useWeiXinPay(serverJsonData) {
			this.$tip.toast('微信支付接入中...')
		},

		//支付宝付款
		useZhiFuBaoPay(serverJsonData) {
			if(!serverJsonData){
				this.$tip.toast('订单异常,请联系客服处理')
				return false
			}
			/*
			后台创建订单返回值如下
				amount: "0.01"
				app_id: "2021003108643486"
				biz_content: "{"out_trade_no":"2022010818165529741599645909","total_amount":"0.01","subject":"1475392291079790594","product_code":"QUICK_MSECURITY_PAY"}"
				notify_url: "https://management.lanmiaoda.com/jeecg-boot/aliapp/payCallback"
				orderInfo: "alipay_sdk=alipay-easysdk-java-2.1.2&app_id=2021003108643486&biz_content=%7B%22out_trade_no%22%3A%222022010818165529741599645909%22%2C%22total_amount%22%3A%220.01%22%2C%22subject%22%3A%221475392291079790594%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=https%3A%2F%2Fmanagement.lanmiaoda.com%2Fjeecg-boot%2Faliapp%2FpayCallback&sign=N4DCgkLOZ0%2BwwsrZRFfR%2FMoFPRi3FHpCmiMsdh1JqBLkelEYDWmufvgYzC1M1UTTnIQXbIphr9IjD8eomhGcXQtsDRtYk5DRkic6d1EogSSqTrudFAJXrNslwFRt5gQGshKOui9kMcOM0ioKgThlWkpXpp3k%2BXCH0uYhN7c2zJfen05V7B69%2BOl3jADVBGhYDfq2v0HLUmtmHb5wX84i0vp0HQuHQDjs%2FirGE54eJCiQB7HH5ZYhM9T2LiaX6hjo7ZJAEDcv7HXkZIFd%2FzthYCEgrcxEEXgUFVXw%2Ft9V0XcUDrp0coWn7AiADACDX97qH2HTLHPaXT5blkGjR0IZLg%3D%3D&sign_type=RSA2&timestamp=2022-01-08+18%3A16%3A55&version=1.0"
				outTradeNo: "2022010818165529741599645909"
				sign: "N4DCgkLOZ0+wwsrZRFfR/MoFPRi3FHpCmiMsdh1JqBLkelEYDWmufvgYzC1M1UTTnIQXbIphr9IjD8eomhGcXQtsDRtYk5DRkic6d1EogSSqTrudFAJXrNslwFRt5gQGshKOui9kMcOM0ioKgThlWkpXpp3k+XCH0uYhN7c2zJfen05V7B69+Ol3jADVBGhYDfq2v0HLUmtmHb5wX84i0vp0HQuHQDjs/irGE54eJCiQB7HH5ZYhM9T2LiaX6hjo7ZJAEDcv7HXkZIFd/zthYCEgrcxEEXgUFVXw/t9V0XcUDrp0coWn7AiADACDX97qH2HTLHPaXT5blkGjR0IZLg=="
				subjectName: "1475392291079790594"
				timestamp: "2022-01-08 18:16:55"
			
			其中orderInfo参数拼接顺序如下
				alipay_sdk=alipay-easysdk-java-2.1.2
				&app_id=2021003108643486
				&biz_content=%7B%22out_trade_no%22%3A%222022010818165529741599645909%22%2C%22total_amount%22%3A%220.01%22%2C%22subject%22%3A%221475392291079790594%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D
				&charset=UTF-8
				&format=json
				&method=alipay.trade.app.pay
				&notify_url=https%3A%2F%2Fmanagement.lanmiaoda.com%2Fjeecg-boot%2Faliapp%2FpayCallback
				&sign=N4DCgkLOZ0%2BwwsrZRFfR%2FMoFPRi3FHpCmiMsdh1JqBLkelEYDWmufvgYzC1M1UTTnIQXbIphr9IjD8eomhGcXQtsDRtYk5DRkic6d1EogSSqTrudFAJXrNslwFRt5gQGshKOui9kMcOM0ioKgThlWkpXpp3k%2BXCH0uYhN7c2zJfen05V7B69%2BOl3jADVBGhYDfq2v0HLUmtmHb5wX84i0vp0HQuHQDjs%2FirGE54eJCiQB7HH5ZYhM9T2LiaX6hjo7ZJAEDcv7HXkZIFd%2FzthYCEgrcxEEXgUFVXw%2Ft9V0XcUDrp0coWn7AiADACDX97qH2HTLHPaXT5blkGjR0IZLg%3D%3D
				&sign_type=RSA2
				&timestamp=2022-01-08+18%3A16%3A55
				&version=1.0
			*/
			let _orderInfo = ''
				
			_orderInfo = serverJsonData.orderInfo
			_orderInfo = 'app_id=2021003112633772&biz_content=%7B%22out_trade_no%22%3A%22f4833085-0c46-4bb0-8e5f-622a02a4cffc%22%2C%22total_amount%22%3A%220.10%22%2C%22subject%22%3A%22iPhone6+16G%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2F47.97.211.24%3A9090%2Fjeecg-boot%2Faliapp%2FpayCallback&sign=dpVGACwYte733Yr8hoT6pVWG1j9Glr2mf1tb273FUFyd6NWynewCUSGcrzEs449aBthWMXoeAQ4o%2B4VNQwCqqiaegx9XN9igQjji1SFVCJ8zEKv31BVL7i3OF87HVhpBJSB4OXx2cVTyLnUoL8345P4kj56W3ziLvYHMng0pDbbLgsfatHIaJGnuy6WJ9SNJM4J2nmXiAqytojq32RTp8u0Hq2PDKg2C6c%2FANS6BvkOHI6cxu5lsJaL7l4ZPEeJnjpZn0teKWci8FP4UozViWFT9x8SrcM%2Fp0Pfzw9BiXJs5VpByPM4DbNM5rwxb7EZgHKMiSXpw7MzeoyYnQKWObw%3D%3D&sign_type=RSA2'
			console.log('自组装 _orderInfo = ', _orderInfo)
			
			//全部使用服务器返回参数
			//this.orderInfo2 = serverJsonData.orderInfo
			this.orderInfo2 = _orderInfo
			uni.showLoading({
				title: '交易处理中...',
				mask: true,
			})
			uni.requestPayment({
				provider: 'alipay',
				orderInfo: orderInfo2, //支付宝订单数据
				success: function(res) {
					uni.hideLoading()
					uni.$tip.success('充值成功')
					this.requestPaymentSuccess = JSON.stringify(res)
					console.log('requestPayment success:' + JSON.stringify(res));
				},
				fail: function(res) {
					uni.hideLoading()
					uni.$tip.success('充值失败')
					this.requestPaymentFail = JSON.stringify(res)
					console.log('requestPayment fail:' + JSON.stringify(res));
				},
				complete: function(res){
					uni.hideLoading()
					this.requestPaymentComplate = JSON.stringify(res)
					console.log('requestPayment complete:' + JSON.stringify(res));
				}
			});
		},

		/**
		 * input处理函数
		 */
		money_input: function(event) {
			this.money = event.target.value;
			//console.log(event.target.value);
		},

		/**
		 * radio处理函数
		 */
		payType_change: function(event) {
			//console.log(event.target.value);
			this.payType = event.target.value;
		},

		/**
		 * voucher_center__55_55处理函数
		 * 数据绑定  data-index="{{index}}" 
		 * 数据取值  var index = e.currentTarget.dataset.index; var cata = this.list_cata_list[index];
		 */
		voucher_center__55_55_click: function(event) {

		},

		/**
		 * voucher_center__58_58处理函数
		 * 数据绑定  data-index="{{index}}" 
		 * 数据取值  var index = e.currentTarget.dataset.index; var cata = this.list_cata_list[index];
		 */
		voucher_center__58_58_click: function(event) {

		},



		//刷新数据
		Refresh: function(_action) {

			uni.showLoading();

			//提交到服务器
			var that = this
			var url = that.apiUrl + '{server_code_file_path}';
			//console.log(url);
			uni.request({
				url: url, //后端接口地址，需要改成自己的接口地址
				data: {
					action: _action, //上传动作，如按钮点击会产生click动作，分页也会产生，在后端根据具体的动作来判断事件
					uid: that.getUid(), //上传用户id,在登录中获得
					//上传页面中的变量
					//定义变量---start
					money: that.money,
					payType: that.payType,

					//定义变量---end
					id: that.id, //上传id值，后端可以根据此值来操作当前id
					del_id: that.del_id, //在列表中删除按钮选中时，上传到有后端，进行删除操作
					page: that.page
				},
				method: 'GET',
				success: function(res) { //后端返回数据

					// 隐藏导航栏加载框  
					//uni.hideNavigationBarLoading();
					// 停止下拉动作  
					uni.stopPullDownRefresh();

					// 隐藏加载框  
					uni.hideLoading();

					var tmp = res.data;

					//初始化，对页面上的控件进行赋值操作
					if (_action == "init") {
						that.payType_array = tmp.payType_array;

					}



					that.money = tmp.money;



					//如果后端有返回消息，则弹出消息提示
					if (tmp.message != null && tmp.message != "") {
						uni.showToast({
							title: tmp.message,
							icon: 'none',
							duration: 2000
						})
					}



					//如果后端有返回页码，则更改当前页码
					if (tmp.page != null && tmp.page != "") {
						page = tmp.page;
					}

				},
				fail: function(res) {
					uni.showToast({
						title: "服务器访问失败",
						icon: 'none',
						duration: 2000
					})
					//console.log(res.data);
					//console.log('is failed')
				}
			})
		},
	}
}
